/*
 * EnigmaGM - A feature rich guild management system.
 * Copyright (C) 2007 ajiaojr.
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */
package org.ajiaojr.enigmagm.client.data.viewer;

import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.SourcesTableEvents;
import com.google.gwt.user.client.ui.TableListener;
import com.google.gwt.user.client.ui.Widget;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author ajiaojr
 */
public class TableViewer extends ContentViewer {
  public static final int DEFAULT_MAX_RESULTS = 30;

  private List columns = new ArrayList();

  private Grid grid = new Grid();

  private List sorters = new ArrayList();

  private TableLabelProvider tableLabelProvider;

  public TableViewer() {
    grid.setWidth("100%");
    grid.addStyleName("gwt-table");
    grid.addStyleName("dataTable");
  }

  public synchronized void addColumn(String columnHeader,
      TableContentSorter sorter) {
    columns.add(columnHeader);
    sorters.add(sorter);
  }

  /*
   * (non-Javadoc)
   * 
   * @see org.ajiaojr.enigmagm.client.data.viewer.ContentListener#contentChanged(java.lang.Object[])
   */
  public void contentChanged(Object[] contents) {
    grid.clear();

    int columnCount = columns.size();

    grid.resize(contents.length + 1, columnCount);

    Iterator it = columns.iterator();
    for (int i = 0; it.hasNext(); i++) {
      grid.setText(0, i, (String) it.next());
      TableContentSorter sorter = (TableContentSorter) sorters.get(i);
      if (sorter != null) {
        grid.getCellFormatter().addStyleName(0, i, "clickable");
      }
    }
    grid.getRowFormatter().addStyleName(0, "header");

    grid.addTableListener(new TableListener() {

      public void onCellClicked(SourcesTableEvents sender, int row, int cell) {
        if (row == 0) {
          for (int i = 0; i < sorters.size(); i++) {
            TableContentSorter sorter = (TableContentSorter) sorters.get(i);
            if (sorter != null) {
              if (i == cell) {
                sorter.trigger();
              } else {
                sorter.reset();
              }
            }
          }
        }
      }
    });

    for (int i = 0; i < contents.length; i++) {
      for (int j = 0; j < columnCount; j++) {
        grid.setText(i + 1, j, tableLabelProvider.getLabel(contents[i], j));
      }
    }
  }

  /**
   * @return
   */
  public Widget getWidget() {
    return grid;
  }

  public void setTableLabelProvider(TableLabelProvider tableLabelProvider) {
    this.tableLabelProvider = tableLabelProvider;
  }
}
